package com.weishuang.monitor.leader.controller;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.weishuang.monitor.leader.commons.entity.BaseEntity;
import com.weishuang.monitor.leader.commons.entity.Result;
import com.weishuang.monitor.leader.commons.enums.SCODE;
import com.weishuang.monitor.leader.commons.utils.ResultUtils;
import com.weishuang.monitor.leader.entity.SysDictType;
import com.weishuang.monitor.leader.entity.dto.DictTypeDTO;
import com.weishuang.monitor.leader.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 数据字典类型 前端控制器
 * </p>
 *
 * @author weishuang
 * @since 2021-03-23
 */
@RestController
@RequestMapping("/leader/sysDictType")
public class SysDictTypeController {

    @Autowired
    private ISysDictTypeService sysDictTypeService;

    /**
     * 分页查询数据字典类型
     * @param dictTypeDTO
     * @return
     */
    @GetMapping("listPageSysDictType")
    public Result listPageSysDictType(DictTypeDTO dictTypeDTO) {
        Page<SysDictType> result = sysDictTypeService.listPageSysDictType(dictTypeDTO);
        return ResultUtils.success(result);
    }

    /**
     * 新增数据字典类型
     * @param sysDictType
     * @return
     */
    @PostMapping
    public Result addDictType(@Validated SysDictType sysDictType) {
        sysDictType.setId(IdWorker.getIdStr());
        sysDictType.insertInit();
        sysDictTypeService.save(sysDictType);
        return ResultUtils.success();
    }

    /**
     * 修改数据字典
     * @param id
     * @param sysDictType
     * @return
     */
    @PutMapping("/{id}")
    public Result updateDictType(@PathVariable String id, @Validated SysDictType sysDictType) {
        sysDictType.setId(id);
        sysDictType.updateInit();
        sysDictTypeService.updateById(sysDictType);
        return ResultUtils.success();
    }

    /**
     * 删除数据字典
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteDictType(@PathVariable String id) {
        LambdaUpdateWrapper<SysDictType> eq = Wrappers.lambdaUpdate(SysDictType.class).set(BaseEntity::getStatus, SCODE.STATUS_DELETE.getValue()).eq(SysDictType::getId, id);
        sysDictTypeService.update(eq);
        return ResultUtils.success();
    }
}
